import { Injectable } from '@angular/core';
import { BaseService } from '@core/routes-services/base.service';
import { CacheService } from '@core/routes-services/cache.service';
import { HttpClient } from '@angular/common/http';
import { myValidNull } from '@core/util/string-utils';

/**
 * 月饼预定 服务
 */
@Injectable()
export class MoonCakeService extends BaseService {

  constructor(http: HttpClient, cacheService: CacheService) {
    super(http, cacheService);
  }

  base_url: any = 'api/market/moon_cake';


  /**
   * 分页
   *
   * @param user
   * @returns {Promise<any>}
   */
  pagination(page: any, keyword: any, paymentMethod: any, status: any, serviceStatus: any, productId: any, voucherStatus: any, sortName: any, sortValue: any): Promise<any> {
    const url = this.base_url + '/pagination';
    if (keyword) {
      page['_filter_like_keyword'] = keyword;
    }
    if (paymentMethod) {
      page['_filter_eq_paymentMethod'] = paymentMethod;
    }
    if (status) {
      page['_filter_eq_status'] = status;
    }
    if (serviceStatus) {
      page['_filter_eq_serviceStatus'] = serviceStatus;
    }
    if (productId) {
      page['_filter_eq_productId'] = productId;
    }
    if (voucherStatus) {
      page['_filter_eq_voucherStatus'] = voucherStatus;
    }
    if (myValidNull(sortName) || myValidNull(sortValue)) {
      page['_sort_by_'] = 'payTime_desc';
    } else {
      page['_sort_by_'] = sortName + '_' + (sortValue == 'ascend' ? 'asc' : 'desc');
    }
    return this.post(url, page);
  }
  /**
   * 订单入账分页
   * 查询所有已确认付款的订单分页做平台入账明细分页
   *
   * @param user
   * @returns {Promise<any>}
   */
  inPagination(page: any, keyword: any, paymentMethod: any): Promise<any> {
    const url = this.base_url + '/in/pagination';
    if (keyword) {
      page['_filter_like_keyword'] = keyword;
    }
    if (paymentMethod) {
      page['_filter_eq_paymentMethod'] = paymentMethod;
    }
    return this.post(url, page);
  }

  /**
   * 修改
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  saveOrUpdateEntity(id: any, entity: any): Promise<any> {
    this.cacheService.setAgentLevel(null);
    if (id) {
      const url = this.base_url + '/' + id;
      return this.put(url, entity);
    } else {
      const url = this.base_url;
      return this.post(url, entity);
    }
  }

  /**
   * 获取查看详情
   *
   * @param id
   * @returns {Promise<any>}
   */
  detail(id: any): Promise<any> {
    const url = this.base_url + '/' + id;
    return this.get(url);
  }

  /**
   * 根据orderNo获取详情
   *
   * @param id
   * @returns {Promise<any>}
   */
  getByOrderNo(orderNo: any): Promise<any> {
    const url = this.base_url + '/actions/order_no/' + orderNo;
    return this.get(url);
  }

  /**
   * 删除
   *
   * @param ids
   * @returns {Promise<any>}
   */
  deleteEntity(ids): Promise<any> {
    this.cacheService.setAgentLevel(null);
    const url = this.base_url + '/';
    return this.delete(url, ids);
  }


  /**
   * 获取全部
   *
   * @returns {Promise<any>}
   */
  getAll(): Promise<any> {
    const url = this.base_url + '/all';
    return this.get(url);
  }

  /**
   * 获取全部
   *
   * @returns {Promise<any>}
   */
  orderRewards(id): Promise<any> {
    const url = this.base_url + '/actions/' + id;
    return this.get(url);
  }

  /**
   * 订单确认发货
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  updateStatus(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/status/' + id;
    return this.put(url, entity);
  }

  /**
   * 3-银行转账 4-支付支付 确认凭证信息
   *
   * @param id 申请充值id
   * @returns {Promise<any>}
   */
  confirmPayAuditing(id: any): Promise<any> {
    const url = this.base_url + '/confirm/payAuditing/' + id;
    return this.put(url, {});
  }

  /**
   * 3-银行转账 4-支付支付 驳回凭证信息
   *
   * @param id 申请充值id
   * @param entity
   * @returns {Promise<any>}
   */
  auditing(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/reject/payAuditing/' + id;
    return this.put(url, { auditingRemark: entity.remark });
  }

  /**
   * 重新发起 现金奖励
   * @param id 现金奖励对应记录id
   * @param entity
   * @returns {Promise<any>}
   */
  reSendRedpack(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/actions/reSendRedpack/' + id;
    return this.put(url, { });
  }
}
